package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        Page<Setmeal> pageinfo = new Page(page,pageSize);
        Page<SetmealDto> pageDto = new Page<>();
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name),Setmeal::getName,name).orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(pageinfo, queryWrapper);
        BeanUtils.copyProperties(pageinfo,pageDto,"records");
        List<Setmeal> records = pageinfo.getRecords();
        List<SetmealDto> List = new ArrayList<>();
        for (Setmeal setmeal:records) {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal,setmealDto);
            Long categoryId = setmeal.getCategoryId();
            Category category = categoryService.getById(categoryId);
            String name1 = category.getName();
            setmealDto.setCategoryName(name1);
            List.add(setmealDto);
        }
        pageDto.setRecords(List);
        return R.success(pageDto);
    }

    /**
     * 新增套餐
     * @param setmealDto
     * @return
     */
    @CacheEvict(value = "setmealCache",allEntries = true)
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        log.info("牛牛牛",setmealDto);
        setmealService.savewithdish(setmealDto);
        return R.success("添加成功");
    }
    @CacheEvict(value = "setmealCache",allEntries = true)
    @DeleteMapping()
    public R<String> deleteByIds(@RequestParam List<Long> ids){
        setmealService.deletewithdish(ids);
        return R.success("删除成功");
    }

    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId+'_'+#setmeal.status")
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        //根据套餐id取到所有的状态在售套餐信息
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(Setmeal::getStatus,1);
        List<Setmeal> setmealList = setmealService.list(queryWrapper);
//        List<SetmealDto> setmealDtoList = new ArrayList<>();
//        setmealDtoList = setmealList.stream().map(item->{
//            SetmealDto setmealDto = new SetmealDto();
//            //提取套餐包含的菜品信息
//            //获取id
//            Long itemId = item.getId();
//            //根据id查询setmealDIsh表中的菜品信息
//            LambdaQueryWrapper<SetmealDish> DishQueryWrapper = new LambdaQueryWrapper<>();
//            DishQueryWrapper.eq(SetmealDish::getSetmealId,itemId);
//            List<SetmealDish> list = setmealDishService.list(DishQueryWrapper);
//            //将套餐信息和套餐包含的菜品信息都封装到setmealdto类中；
//            BeanUtils.copyProperties(item,setmealDto);
//            setmealDto.setSetmealDishes(list);
//           return setmealDto;
//        }).collect(Collectors.toList());
        return R.success(setmealList);
    }
}
